package com.cloudpolice.weixin.unit;


import com.cloudpolice.weixin.model.LogResult;
import com.cloudpolice.weixin.model.SessionAndOpenModel;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tool.RedisAction;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 梁昊
 * @date 2020/2/19
 * @function
 * @editLog
 */
@Component
public class RedisOperator {
    @Autowired
    RedisAction redisAction;
    @Autowired
    Gson gson;

    public String getSessionKeyAndOpenId(String appId
            , String code
    ) {
        String keyName = String.format("%s:%s", appId, code);
        return redisAction.getValue(keyName);
    }

    public void saveSessionKeyAndOpenId(String appId
            , String code
            , String openId
            , String sessionKey
            , long expirationTime
    ) {
        SessionAndOpenModel sessionAndOpenModel = new SessionAndOpenModel();
        sessionAndOpenModel.setOpenId(openId);
        sessionAndOpenModel.setSessionKey(sessionKey);
        String keyName = String.format("%s:%s", appId, code);
        redisAction.saveKeyAndValue(keyName, gson.toJson(sessionAndOpenModel), expirationTime, TimeUnit.HOURS);
    }
}
